package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Dep;
import com.itheima.domain.Patient;
import com.itheima.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;
    @RequestMapping("/findByName")
    public ModelAndView findByName(String tname){
        ModelAndView modelAndView = new ModelAndView();
        List<Patient> patientList = patientService.findByName(tname);
        System.out.println(patientList);
        modelAndView.addObject("patientList",patientList);
        modelAndView.setViewName("/pages/patients-list.jsp");
        return modelAndView;

    }

    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        List<Patient> patientList = patientService.findAll();
        modelAndView.addObject("patientList",patientList);
        modelAndView.setViewName("/pages/patients-list.jsp");
        return modelAndView;

    }

    @RequestMapping("/save")
    public String save(Patient patient){
        patientService.save(patient);
        return "redirect:/patient/findAll";
    }

    @RequestMapping("/findDepAll")
    @ResponseBody
    public List<Dep> findDepAll() throws JsonProcessingException {

        List<Dep> depList = patientService.findDepAll();
        System.out.println(depList);

        return depList;
    }
}
